6c3e53faa2bee7448f4658a238ff0b0d72a30ee6,app/src/test/java/com/codemonkeylabs/encryptionsexample/EncryptionTest.java,EncryptionTest,testRSAandAESEncryption,#,77
Before Change
@Test
public void testRSAandAESEncryption()
{
AESEncryptDecrypt aesEncryptDecrypt = new AESEncryptDecrypt();
RSAEncryptDecrypt rsaEncryptDecrypt = new RSAEncryptDecrypt();
String encryptedString = aesEncryptDecrypt.encrypt(testText,
AESEncryptDecrypt.NOT_SECRET_ENCRYPTION_KEY.getBytes(),
AESEncryptDecrypt.IVS.getBytes(),
AESEncryptDecrypt.AESCipherType.AES_CIPHER_CTR_NOPADDING);
byte[] combined = concat(AESEncryptDecrypt.NOT_SECRET_ENCRYPTION_KEY.getBytes(), AESEncryptDecrypt.IVS.getBytes());
byte[] encryptedAESKey = rsaEncryptDecrypt.encrypt(combined);
byte[] unencryptedAESKey = rsaEncryptDecrypt.decrypt(encryptedAESKey);
byte[] aesKey = Arrays.copyOfRange(unencryptedAESKey, 0, 16);
byte[] ivs = Arrays.copyOfRange(unencryptedAESKey, 16, 32);
String unencryptedString = aesEncryptDecrypt.decrypt(encryptedString,
aesKey,
ivs,
AESEncryptDecrypt.AESCipherType.AES_CIPHER_CTR_NOPADDING);
assertTrue(unencryptedString.startsWith("All this while Tashtego, Daggoo, and Queequeg"));
}
After Change
RSAEncryptDecrypt rsaEncryptDecrypt = new RSAEncryptDecrypt();
ByteArrayInputStream plainTextInputStream = new ByteArrayInputStream(testText.getBytes("UTF-8"));
ByteArrayOutputStream encOutputStream = new ByteArrayOutputStream(1024 * 100);
byte[] iv = AESEncryptDecrypt.aesEncrypt(plainTextInputStream,
AESEncryptDecrypt.NOT_SECRET_ENCRYPTION_KEY.toCharArray(),
AESEncryptDecrypt.AESCipherType.AES_CBC_PKCS5PADDING,
encOutputStream);
byte[] combined = Util.concat(AESEncryptDecrypt.NOT_SECRET_ENCRYPTION_KEY.getBytes(),
iv);
byte[] encryptedAESKey = rsaEncryptDecrypt.encrypt(combined);
byte[] unencryptedAESKey = rsaEncryptDecrypt.decrypt(encryptedAESKey);
byte[] aesKey = Arrays.copyOfRange(unencryptedAESKey, 0, 32);
byte[] ivs = Arrays.copyOfRange(unencryptedAESKey, 32, 48);
ByteArrayInputStream encInputStream = new ByteArrayInputStream(encOutputStream.toByteArray());
ByteArrayOutputStream plainTextOutputStream = new ByteArrayOutputStream(1024 * 100);
AESEncryptDecrypt.aesDecrypt(encInputStream,
new String(aesKey, "UTF-8").toCharArray(),
ivs,
AESEncryptDecrypt.AESCipherType.AES_CBC_PKCS5PADDING,
plainTextOutputStream);
String unencryptedString = new String(plainTextOutputStream.toByteArray(),"UTF-8");
assertTrue(unencryptedString.startsWith("All this while Tashtego, Daggoo, and Queequeg"));
}